## 规定 terraform 版本
terraform_version_constraint = ">= 1.2.3"

terraform_binary = "tf"

## 规定 tfwrapper 版本
tfwrapper_version_constraint = ">= 0.1.0"

## 是否开启 auto-inputs 会自动查看input和output进行设置
auto_inputs = true

## When passed in, limit the number of modules that are run concurrently to this number during run commands.
parallelism = 3

download_dir = "${get_repo_root()}/.cache"
#non_interactive = true

globals {
  github_token = "${env.GITHUB_TOKEN}"
}

inputs {
  github_token = global.github_token
}

stacks {
  # 默认起效的
  default = "app"
  stack "test" {
    source = ["stacks/test", "stacks/bases"]
  }
  stack "staging" {
    source = ["stacks/staging", "stacks/bases"]
  }
  stack "app" {
    source = ["stacks/app"]
  }
}

function "get_version" {
  command = "${get_repo_root()}/cicd/deploy/version.sh"
  args    = ["${local.stack_name}"]
  env     = {
    "GITHUB_TOKEN" = global.github_token
  }
}

## 全局设置
terraform {
  extra_arguments "common_vars" {
    commands  = get_terraform_commands_that_need_vars()
    arguments = [
      "-var-file=${get_tfwrapper_dir()}/config.tfvars"
    ]
    optional_var_files = ["${get_tfwrapper_dir()}/optional.tfvars"]
  }

  extra_arguments "auto-init" {
    commands  = ["init"]
    arguments = ["-reconfigure", "-upgrade"]
  }

  #  extra_arguments "auto-approve" {
  #    commands  = ["apply", "destroy"]
  #    arguments = ["-auto-approve"]
  #  }

  after_hook "backup-state" {
    commands     = ["apply", "refresh", "destroy"]
    execute      = ["ls", "-la"]
    run_on_error = true
  }

  before_hook "fetch_versions" {
    commands = ["init"]
    execute  = ["ls"]
  }
}

encryption {
  enabled  = true
  key_path = "${env.HOME}/.ssh/id_rsa"
}

generate "state" {
  exclude   = ["module.network"]
  path      = "backend.tf"
  if_exists = "error" # overwrite, error, skip
  content {
    terraform {
      backend "local" {
        path = "${get_repo_root()}/.state/${get_module_dir()}/${get_stack()}.tfstate"
      }
    }
  }
}
